package cn.kgc.fcm.mybatisplusgeneratordemo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

public class MyBatisPlusGenerator {
	public static void main(String[] args) {
//1. 全局配置
		GlobalConfig config = new GlobalConfig();
		config.setActiveRecord(true)
				.setAuthor("christopher")
				.setOutputDir("C:/demo/src/main/java")
				.setFileOverride(true)
				.setIdType(IdType.AUTO)
				.setDateType(DateType.ONLY_DATE)
				.setServiceName("%sService")
				.setEntityName("%s")
				.setBaseResultMap(true)
				.setActiveRecord(false)
				.setBaseColumnList(true)
		;
		
//2. 数据源配置
		DataSourceConfig dsConfig = new DataSourceConfig();
		// 设置数据库类型
		dsConfig.setDbType(DbType.MYSQL)
				.setDriverName("com.mysql.cj.jdbc.Driver")
				.setUrl("jdbc:mysql:///ds_bank")
						.setUsername("root")
						.setPassword("root");

//3. 策略配置globalConfiguration中
		StrategyConfig stConfig = new StrategyConfig();
//全局⼤写命名
		stConfig.setCapitalMode(true)
				.setNaming(NamingStrategy.underline_to_camel)
				.setEntityLombokModel(true)
				.setRestControllerStyle(true)
		//	.setInclude("表名")
				.setInclude(scanner("表名，多个英⽂逗号分割").split(","));
		//去掉数据表前缀
		stConfig.setTablePrefix("t_");
		PackageConfig pkConfig = new PackageConfig();
		pkConfig.setParent("cn.kgc.fcm.chlm")
				.setMapper("mapper")
				.setService("service")
				.setController("controller")
				.setEntity("entity")
				.setXml("mapper");

//5. 整合配置
		AutoGenerator ag = new AutoGenerator();
		ag.setGlobalConfig(config)
				.setDataSource(dsConfig)
				.setStrategy(stConfig)
				.setPackageInfo(pkConfig);

//6. 执⾏操作
		ag.execute();
		System.out.println("======= 相关代码⽣成完毕 ========");
	}
	/**
	 * <p>
	 * 读取控制台内容
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输⼊" + tip + "： ");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			
			if (StringUtils.isNotBlank(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输⼊正确的" + tip + "！ ");
	}
}